/* ====================================================================
 * (#)Header: SenderBean.java
 * (#)Revision: 
 * (#)Date Created: 29-Oct-09 12:47:15 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.jms.mdb;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * <code>SenderBean</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
@Stateless
public class SenderBean implements ClientLocal {

	@Resource
	private ConnectionFactory connectionFactory;

	@Resource(name = "queue/ForumChannel")
	private Queue queue;

	public void sendMessage(String text) throws JMSException {

		Connection connection = null;
		Session session = null;

		try {
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer producer = session.createProducer(queue);
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			TextMessage message = session.createTextMessage(text);
			producer.send(message);
		} finally {
			if (session != null)
				session.close();
			if (connection != null)
				connection.close();
		}
	}
}
